home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ABUSESRC.ZIP / AbuseSrc / macabuse / imlib / lbmread.c < prev    next >
C/C++ Source or Header  |  1997-05-20  |  2KB  |  108 lines

  1. #include "lbmread.hpp"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "system.h"
  5.  
  6.  
  7.  
  8. image *read_lbm(char *filename, palette *&pal)
  9. {
  10.   FILE *fp=fopen("/cam/amur/set/city/city1.lbm","rb");
  11.   char type[4];
  12.   image *im=NULL;
  13.   fread(type,1,4,fp);
  14.   if (memcmp(type,"FORM",4))
  15.   {
  16.     set_error(imNOT_SUPPORTED);
  17.     fclose(fp);
  18.     return NULL;
  19.   }
  20.   else
  21.   {
  22.     long size=read_other_long(fp);
  23.     fread(type,1,4,fp);    
  24.     if (memcmp(type,"PBM ",4))
  25.     {
  26.       set_error(imNOT_SUPPORTED);
  27.       fclose(fp);
  28.       return NULL;
  29.     }
  30.     else
  31.     {
  32.       long offset=12,ssize;    
  33.       char stype[4];
  34.       short w=0,h=0,x,y,tcolor,pagew,pageh;
  35.       char planes,masking,compr,padl,xa,ya;
  36.       
  37.       while (ftell(fp)+4<size)
  38.       {
  39.     fread(stype,1,4,fp);
  40.     ssize=read_other_long(fp);
  41.         if (ssize &1) ssize++;            // specs say all chunks are padded by 2
  42.     if (!memcmp(stype,"BMHD",4))
  43.     {
  44.       w=read_other_short(fp);
  45.       h=read_other_short(fp);
  46.       x=read_other_short(fp);
  47.       y=read_other_short(fp);
  48.       planes=fgetc(fp);
  49.       masking=fgetc(fp);
  50.       compr=fgetc(fp);
  51.       padl=fgetc(fp);
  52.       tcolor=read_other_short(fp);
  53.       xa=fgetc(fp);
  54.       ya=fgetc(fp);
  55.       pagew=read_other_short(fp);
  56.       pageh=read_other_short(fp);
  57.     } else if (!memcmp(stype,"CMAP",4))
  58.     {
  59.       pal=new palette(256);
  60.       fread(pal->addr(),1,768,fp);
  61.     } else if (!memcmp(stype,"BODY",4) && w && h)  // make sure we read a BHMD before
  62.     {
  63.       if (im) delete im;  // shouldn't be two BODY's butjust in case...
  64.       im=new image(w,h);
  65.       int x,y;
  66.       if (!compr)
  67.       {
  68.         for (y=0;y<h;h++)
  69.           fread(im->scan_line(y),1,w,fp);
  70.       } else
  71.       {
  72.         for (y=0;y<h;y++)       
  73.         {
  74.           int c,i,n=0;
  75.           unsigned char *sl=im->scan_line(y);
  76.           do 
  77.           {
  78.         c=fgetc(fp)&0xff;
  79.         if (c&0x80)
  80.         {
  81.           if (c!=0x80)
  82.           {
  83.             i=((~c)&0xff)+2;
  84.             c=fgetc(fp);
  85.             while (i--) sl[n++]=c;
  86.           }
  87.         }
  88.         else
  89.         {
  90.           i=c+1;
  91.           while (i--) sl[n++]=fgetc(fp);
  92.         }
  93.           } while (n<w);
  94.         }
  95.       }      
  96.     }
  97.     else
  98.       fseek(fp,ssize,SEEK_CUR);
  99.       }
  100.     }
  101.   }
  102.   fclose(fp);
  103.   if (!im) set_error(imFILE_CORRUPTED);
  104.   return im;
  105. }
  106.  
  107.  
  108.